package com.sanerzone.smscenter.gateway.http.handler.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.sanerzone.smscenter.biz.message.SMSMOMessage;
import com.sanerzone.smscenter.biz.message.SMSMTMessage;
import com.sanerzone.smscenter.biz.message.SMSRTMessage;
import com.sanerzone.smscenter.biz.message.SMSSRMessage;
import com.sanerzone.smscenter.gateway.http.handler.GateWayHttpAbstract;
import com.sanerzone.smscenter.gateway.http.message.SmsHttpMsgResponse;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class SmsApiJIESMSV2 extends GateWayHttpAbstract
{

    public SmsApiJIESMSV2(String gateWayID, String jsonParams)
    {
        super(gateWayID, jsonParams);
    }

    @Override
    public List<SMSSRMessage> doSend(SMSMTMessage mt)
    {
        String json = "";
        List<SMSSRMessage> resultList = Lists.newArrayList();
        SMSSRMessage message = new SMSSRMessage();
        message.setPhones(mt.getPhone());
        message.setMessage(mt);
        message.setFailPhone("");
        message.setResult("0");
        message.setMsgid(mt.getMtBatchNum());
        resultList.add(message);
        return resultList;
    }
    
    @Override
    public SmsHttpMsgResponse parseReport(String text, List<SMSRTMessage> rtList)
    {


        logger.info("{}网关推送状态报告:{}", gateWayID, text);
        try {
        	if(StringUtils.isNotBlank(text)) {
                JSONArray result = JSON.parseArray(text);
                for(int i=0 ; i < result.size() ;i++){
                    JSONObject data = result.getJSONObject(i);
                    SMSRTMessage smsRtMessage = initSmsRtMessage(data.getString("taskid"), data.getString("phone"),data.getString("phone"),
                            data.getString("code"), "0");
                    rtList.add(smsRtMessage);
                }
        	}
        } catch(Exception e) {
        	 return new SmsHttpMsgResponse("ERR");
        }
        return new SmsHttpMsgResponse("OK");
    }

    @Override
    public SmsHttpMsgResponse parseDeliver(String text, List<SMSMOMessage> moList)
    {
    	logger.info("{}网关推送用户上行:{}", gateWayID, text);
        try {
        	if(StringUtils.isNotBlank(text)) {
        	    JSONObject object = JSON.parseObject(text);
                SMSMOMessage smsMoMessage = new SMSMOMessage();
                smsMoMessage.setSpnumber(object.getString("spnumber"));
                smsMoMessage.setPhone(object.getString("phone"));
                smsMoMessage.setMsgContent(object.getString("msg"));
                smsMoMessage.setMsgid(object.getString("msgid"));
                smsMoMessage.setGateWayID(gateWayID);
                moList.add(smsMoMessage);
        	}
        } catch (Exception e) {
        	e.printStackTrace();
            return new SmsHttpMsgResponse("ERROR");
        }
        return new SmsHttpMsgResponse("OK");
    }

    @Override
    public String queryReport(String jsonText)
    {
        return null;
    }

    @Override
    public String queryDeliver(String jsonText)
    {
        return null;
    }
    
    public String balance()
    {
        return "";
    }
    
    public static void main(String[] args)
    {
    	SmsApiJIESMSV2 api = new SmsApiJIESMSV2("JIESMS", "{\"account\":\"lihua\",\"password\":\"llh@9988\",\"longSms\":\"\",\"apiUrl\":\"http://www.88dx.cn\",\"reportSecTime\":\"5\", \"deliverSecTime\":\"5\"}");
    	SMSMTMessage mt = new SMSMTMessage();
    	mt.setPhone("18268846802");
    	mt.setSmsContent("【千橡科技】验证码：123456，10分钟内有效，如非本人操作，请忽略！");
    	List<SMSSRMessage> list = api.doSend(mt);
    	System.out.println(JSON.toJSONString(list));
    }
    
}